package karaokit.com;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RadioGroup;

public class Searcher  extends Activity implements OnClickListener {

	ProgressBar mProgBar;
	Button mSearchButton;
	RadioGroup mRadioGruop;
	EditText mSearchText;
	
	static final String MEDIA = "midi";
	static final String LYRIC = "lrc";	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_page);
		
		mSearchText = (EditText)this.findViewById(R.id.SearchText);
		mSearchButton = (Button)this.findViewById(R.id.SearchButton);
		mSearchButton.setOnClickListener(this);
		
		mProgBar = (ProgressBar)this.findViewById(R.id.SearchProgressBar);
		
		mRadioGruop = (RadioGroup)this.findViewById(R.id.SearchRadioGroup);
	}
	
	@Override
	protected void onResume() {
		mSearchButton.setEnabled(true);
		mProgBar.setVisibility(View.INVISIBLE);
		super.onResume();
	}
	
	public void onClick(View v) {
		switch (v.getId()) {
			case (R.id.SearchButton) :				
				mProgBar.setVisibility(View.VISIBLE);				
				mSearchButton.setEnabled(false);
				
				String search = mSearchText.getText().toString();
				String type = (R.id.MediaRadioButton == mRadioGruop.getCheckedRadioButtonId()) ?
							MEDIA : LYRIC;
				Intent i = new Intent(Intent.ACTION_VIEW);
				Uri uri = Uri.parse("http://www.google.com/search?q=\"" + 
						search.replaceAll(" ", "+") + "\"+" + type + "+download");
				i.setData(uri);
				startActivity(i);
				break;
		}		
	}	

}
